﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Globalization;
using Pixysoft.Framework.Logs.Controller;

namespace Pixysoft.Framework.Logs.Core
{
    class LogReport : ILogReport
    {
        IXmlNode node;

        public LogReport()
        {
            node = XmlManager.CreateNode(StringHelper.default_element_report);
            node.Properties.Add(StringHelper.default_attribute_time, StringHelper.GetRawDateTime());
        }

        public string Id
        {
            get { return node.Properties[StringHelper.default_attribute_id]; }
            internal set { node.Properties[StringHelper.default_attribute_id] = value; }
        }

        public int TimeSpan
        {
            get
            {
                return node.Properties.Get<int>(StringHelper.default_attribute_timespan);
            }
            internal set
            {
                node.Properties[StringHelper.default_attribute_timespan] = value.ToString();
            }
        }

        public DateTime TimeFrom
        {
            get
            {
                return node.Properties.Get<DateTime>(StringHelper.default_attribute_timefrom, StringHelper.default_log_dateformat);
            }
            internal set { node.Properties[StringHelper.default_attribute_timefrom] = StringHelper.GetRawDateTime(value); }
        }

        public DateTime TimeTo
        {
            get
            {
                return node.Properties.Get<DateTime>(StringHelper.default_attribute_timeto, StringHelper.default_log_dateformat);
            }
            internal set { node.Properties[StringHelper.default_attribute_timeto] = StringHelper.GetRawDateTime(value); }
        }

        public DateTime CreateDate
        {
            get
            {
                return node.Properties.Get<DateTime>(StringHelper.default_attribute_time, StringHelper.default_log_dateformat);
            }
        }

        public ILogRecord Record
        {
            get
            {
                if (!node.Node.Contains(StringHelper.default_element_log))
                    return null;

                return new LogRecord(node.Node[StringHelper.default_element_log]);
            }
            internal set
            {
                node.Node.Remove(StringHelper.default_element_log);

                if (value == null)
                    return;

                IXmlNode subnode = node.Node.Add(StringHelper.default_element_log);

                LogRecord record = new LogRecord(subnode);
                record.Filepath = value.Filepath;
                record.Frames = value.Frames;
                record.Id = value.Id;
                record.Ip = value.Ip;
                record.Level = value.Level;
                record.RawDate = value.RawDate;
                record.Text = value.Text;
            }
        }

        public ILogDocument Document
        {
            get
            {
                if (!node.Node.Contains(StringHelper.default_element_document))
                    return null;

                return new LogDocument(node.Node[StringHelper.default_element_document]);
            }
            internal set
            {
                node.Node.Remove(StringHelper.default_element_document);

                if (value == null)
                    return;

                IXmlNode subnode = node.Node.Add(StringHelper.default_element_document);

                LogDocument record = new LogDocument(subnode);
                record.Filename = value.Filename;
                record.Filepath = value.Filepath;
                record.Records = value.Records;
            }
        }

        public ILogDocument[] FrameDocuments
        {
            get
            {
                if (!node.Nodes.Contains(StringHelper.default_element_framedocument))
                    return new ILogDocument[] { };

                List<ILogDocument> list = new List<ILogDocument>();
                foreach (IXmlNode subnode in node.Nodes[StringHelper.default_element_framedocument])
                {
                    list.Add(new LogDocument(subnode));
                }
                return list.ToArray();
            }
            internal set
            {
                node.Nodes.Remove(StringHelper.default_element_framedocument);

                if (value == null)
                    return;

                foreach (ILogDocument doc in value)
                {
                    IXmlNode subnode = node.Nodes.Add(StringHelper.default_element_framedocument);
                    LogDocument framedoc = new LogDocument(subnode);
                    framedoc.Filename = doc.Filename;
                    framedoc.Filepath = doc.Filepath;
                    framedoc.Records = doc.Records;
                }
            }
        }

        public string RawSerialize()
        {
            return LogNonStructureSerializeController.Instance.Serialize(this);
        }

        public string Serialize()
        {
            return Serialize(false);
        }

        public string Serialize(bool indent)
        {
            return node.Serialize(indent);
        }
    }
}
